package com.itany.life.userdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.life.userdemo.dao.UserDao;
import com.itany.life.userdemo.entity.User;
import com.itany.life.userdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public PageInfo<User> find(Integer page, Integer rows, User user) {

        PageHelper.startPage(page,rows);
//        List<User> users=userDao.select(name);


        LambdaQueryWrapper<User> userWrapper=new LambdaQueryWrapper<User>();
        if(user.getName()!=null&&!"".equals(user.getName().trim())){
            userWrapper.eq(User::getName,user.getName());
        }
        if(user.getPhone()!=null&&!"".equals(user.getPhone().trim())){
            userWrapper.eq(User::getPhone,user.getPhone());
        }
        if(user.getWechat()!=null&&!"".equals(user.getWechat().trim())){
            userWrapper.eq(User::getWechat,user.getWechat());
        }
        if(user.getQq()!=null&&!"".equals(user.getQq().trim())){
            userWrapper.eq(User::getQq,user.getQq());
        }
        List<User> users=userDao.selectList(userWrapper);


        return new PageInfo<>(users);
    }

    @Override
    public void modifyFlag(Integer id, Integer flag) {

        LambdaUpdateWrapper<User> userWrapper=new LambdaUpdateWrapper<User>();
        userWrapper.set(User::getFlag,flag);
        userWrapper.eq(User::getId,id);
        userDao.update(null,userWrapper);

    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public User findById(Integer id) {
        return userDao.selectById(id);
    }

    @Override
    public void modify(User user) {
        userDao.updateById(user);
    }
}
